#ifndef FRMMAIN_H
#define FRMMAIN_H

#include <QMainWindow>
#include <QSerialPort>
#include <QTcpSocket>
#include <QPushButton>
#include <QButtonGroup>
#include "ui_frmmain.h"

class frmserialport;
class frmtcpclient;
class frmdeviceconfig;
class frmdeviceupdate;

class frmmain : public QMainWindow
{
    Q_OBJECT

public:
    explicit frmmain(QWidget *parent = nullptr);
    ~frmmain();

    bool getIsSerialConnected() const { return isSerialConnected; }
    bool getIsTcpConnected() const { return isTcpConnected; }

    void setIsSerialConnected(bool connected) {
        isSerialConnected = connected;
        emit serialConnectionChanged(connected);
    }
    void setIsTcpConnected(bool connected) {
        isTcpConnected = connected;
        emit tcpConnectionChanged(connected);
    }

    QSerialPort* getSerialPort() { return &serialPort; }
    QTcpSocket* getTcpSocket() { return &tcpSocket; }

signals:
    void serialConnectionChanged(bool connected);
    void tcpConnectionChanged(bool connected);
    void serialDataReceived(const QString &data); // 新增信号，通知串口数据接收
    void tcpDataReceived(const QString &data);    // 新增信号，通知 TCP 数据接收

private slots:
    void on_serialPort_readyRead();
    void on_tcpSocket_connected();
    void on_tcpSocket_disconnected();
    void on_tcpSocket_readyRead();
    void on_navButtonClicked(int index); // 新增槽函数，处理导航按钮点击

private:
    Ui::frmmain *ui;
    QButtonGroup *navButtonGroup;
    QSerialPort serialPort;
    QTcpSocket tcpSocket;
    bool isSerialConnected;
    bool isTcpConnected;
    QString serialBuffer; // 新增串口数据缓冲区
    QString tcpBuffer;    // 新增 TCP 数据缓冲区

    frmserialport *serialPortPage;
    frmtcpclient *tcpClientPage;
    frmdeviceconfig *deviceConfigPage;
    frmdeviceupdate *deviceUpdatePage;
};

#endif // FRMMAIN_H